package com.tjtt.tdm.base;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.log4j.Logger;

public class GenerateSequenceUtil {

    /** .log */
    private static final Logger logger = Logger.getLogger(GenerateSequenceUtil.class);

    /** The FieldPosition. */
    private static final FieldPosition HELPER_POSITION = new FieldPosition(0);

    /** This Format for format the data to special format. */
    private final static Format dateFormat = new SimpleDateFormat("yyMMddHHmmssSSS");

    /** This Format for format the number to special format. */
    private final static NumberFormat numberFormat = new DecimalFormat("000");

    /** This int is the sequence number ,the default value is 0. */
    private static int seq = 0;

    private static final int MAX = 999;//随机数最大值
    //9223372036854775807
    //150729143040855778
    /**
     * 时间格式生成序列
     * @return String
     */
    public static synchronized String generateSequenceNo() {

        Calendar rightNow = Calendar.getInstance();

        StringBuffer sb = new StringBuffer();

        dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);

        numberFormat.format(seq, sb, HELPER_POSITION);

        if (seq == MAX) {
            seq = 0;
        } else {
            seq++;
        }

       // logger.info("THE SQUENCE IS :" + sb.toString());

        return sb.toString();
    }
    
    public static void main(String[] args){
//    	for(int i = 0 ; i<1000 ;i++){
//    		//System.out.println("第"+i+"个ID：" + generateSequenceNo());
//    		//System.out.println("第"+i+"个ID：" + generateSequenceNo());
//    	}
    	String id = "150729144032437096";
		//System.out.println(new BigDecimal(id));
    }
}